package com.turing.post.organization.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.CreditAccDetail;
import com.turing.post.organization.util.CreditAccDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Mar 15, 2013
 */
@Stateless(mappedName = "organization.impl.CreditAccDetailDaoImpl")
@Remote
@Local
public class CreditAccDetailDaoImpl implements CreditAccDetailDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insertCreditAccDetail(CreditAccDetail cad)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, cad);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CreditAccDetail> getCreditAccDetailList(PCPSWebInput webInput,
			String branchNo, String startDate, String endDate, String TranType,
			Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from CreditAccDetail t where (?1 is null or TranDate>=to_date(?1,'yyyy-mm-dd')) and (?2 is null or TranDate<=to_date(?2,'yyyy-mm-dd')) and (?3 is null or TranType=?3) ";
		sql += PCPSUtil.getSqlCond(webInput, branchNo, "IssueBranchNo");
		sql+="order by tranTime desc";
		Query query = _em.createNativeQuery(sql, CreditAccDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, TranType);
		return query.getResultList();
	}

	@Override
	public Integer getCreditAccDetailTotal(PCPSWebInput webInput, String branchNo,
			String startDate, String endDate, String TranType) {
		String sql = "select nvl(count(*),0) from CreditAccDetail where (?1 is null or TranDate>=to_date(?1,'yyyy-mm-dd')) and (?2 is null or TranDate<=to_date(?2,'yyyy-mm-dd')) and (?3 is null or TranType=?3) ";
		sql += PCPSUtil.getSqlCond(webInput, branchNo, "IssueBranchNo");
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		query.setParameter(3, TranType);
		return Integer.parseInt((query.getSingleResult().toString()));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getDeliverAndActivateByIssueBrchNo(
			String issueBranchNo, Date tranDate) {
		String sql = "select nvl(sum(case when t.trantype='10' then t.amount else 0 end),0),"
				+ " nvl(sum(case when t.trantype='40' then t.amount else 0 end),0)"
				+ " from creditaccdetail t"
				+ " where t.issuebranchno=?1 and t.trandate = ?2";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueBranchNo);
		query.setParameter(2, tranDate);
		return query.getResultList();
	}
}
